$(function () {
    layui.use('form', function(){
        var form = layui.form;
        
        //各种基于事件的操作，下面会有进一步介绍
        form.verify({
            repwd:function (value,item){
                console.log($('#form_reg [name=password]').val());
                console.log(value);
                if(value != $('#form_reg [name=password]').val()){
                    return '确认密码必须保持一致'
                }
            }
        })
      });

    console.log('hello');
    $('#link_reg').on('click', function () {
        $('.login-box').hide()
        $('.reg-box').show()
    });
    $('#link_login').on('click', function () {
        $('.reg-box').hide()
        $('.login-box').show()
    });

    // 注册表单提交事件
    $('#form_reg').on('submit', function (e) {
        e.preventDefault()
        var username = $('#form_reg [name=username]').val()
        var password = $('#form_reg [name=password]').val()
        axios.post("/api/reguser", `username=${username}&password=${password}`)
            .then(res => {
                layui.use('layer', function () {
                    var layer = layui.layer;
                        layer.msg(res.data.message)
                })
                $('#link_login').click()
            })
            .catch(err => {
                layui.use('layer', function () {
                    var layer = layui.layer;
                        layer.msg(res.data.message)
                })
            })
    })
    $('#form_login').on('submit', function (e) {
        e.preventDefault()
        var username = $('#form_login [name=username]').val()
        console.log(username);
        var password = $('#form_login [name=password]').val()
        axios.post("/api/login", `username=${username}&password=${password}`)
            .then(res => {
                if (res.data.status == 0) {
                    localStorage.setItem('token',res.data.token)
                    
                    layui.use('layer', function () {
                        var layer = layui.layer;
                            layer.msg(res.data.message)
                    })
                    setTimeout("location.href = 'index.html'",1000)
                    console.log(res.data);
                }
            })
            .catch(err => {
                mylayer(res.data.message)
            })
    })
})